package com.swh.spit.service;

import com.swh.spit.dao.SpitDao;
import com.swh.spit.entity.spit;
import com.swh.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description tb_pl 服务层
 * @author swh
 * @date 2021-01-13 17:36:42
*/
@Service
public class SpitService {

    @Autowired
    SpitDao spitDao;

    @Autowired
    IdUtil idUtil;

    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 分享成功+1
     * thumbup+1
     * @return
     **/
    public void share(String share){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(share));

        Update update = new Update();
        update.inc("share",1);

        mongoTemplate.updateFirst(query,update,"spit");
    }



    /**
     * 增加浏览量
     * thumbup+1
     * @return
     **/
    public void views(String v){
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(v));
        Update update = new Update();
        update.inc("visits",1);
        mongoTemplate.updateFirst(query,update,"spit");
    }

    /**
     * 发布吐槽
     * @return
     **/
    public void save(spit spsit) {
        spsit.setId(idUtil.nextId() + "");
        spsit.setPublishtime(new Date());//发布日期
        spsit.setVisits(0);//浏览量
        spsit.setShare(0);//分享数
        spsit.setThumbup(0);//点赞数
        spitDao.save(spsit);

    }


    /**
     * 取消吐槽点赞
     * thumbup-1
     * @return
     **/
    public void cancelTeasing(String spitid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(spitid));
        Update update = new Update();
        update.inc("thumbup",-1);
        mongoTemplate.updateFirst(query,update,"spit");

    }

    /**
     * 吐槽点赞
     * thumbup+1
     * @return
     **/
    public void thumbup(String spitid) {
        //创建查询对象
        Query query = new Query();
        //拼接条件
        query.addCriteria(Criteria.where("_id").is(spitid));
        //创建修改对象
        Update update = new Update();
        update.inc("thumbup",1);
        //db.spit.update({_id:"1144814063860137984"},{$inc:{thumbup:NumberInt(1)}})
        // 拼接条件和修改结果,选中要改的文档(集合)
        mongoTemplate.updateFirst(query,update,"spit");

    }



    /**
     * 查询全部
     * @return
     **/
    public List<spit> findAll(){
       return spitDao.findAll();
    }

    /**
     * 根据id查询
     * @return
     **/
    public spit findById(String id){
        return spitDao.findById(id).get();
    }

    /**
     * 修改
     * @return
     **/
    public void update(spit spit){
        spitDao.save(spit);
    }

    /**
     *  删除
     * @return
     **/
    public void delete(String id){
        spitDao.deleteById(id);
    }

    /**
     * 增加
     * @return
     **/
    public void insert(spit spit){
        spit.setId(idUtil.nextId());
        spit.setContent("人生自古谁无死");
        spit.setUserid("5");
        spit.setNickname("留取丹心照汗青");
        spit.setVisits(6000);
        spitDao.save(spit);
    }

}
